/* eslint-disable no-debugger */
/* eslint-disable no-nested-ternary */
/* eslint-disable jsx-a11y/label-has-associated-control */
// eslint-disable-next-line jsx-a11y/label-has-associated-control

/**
 * 表单字段控件只读模式
 * @author LiAnqi
 * @date 2022-6-10
 * @description
 */

import moment from 'moment'
import { ControlType as TYPE, FORMAT } from '../../config/Constant'

function FieldText(props) {
  const {
    name,
    value,
    withText = false,
    text = null,
    controlType = TYPE.INPUT,
    options,
    codeKey,
  } = props

  const findTextFromOptions = (code, opts) => {
    if (opts) {
      const fArr = opts?.filter((item) => item.code === code)
      if (fArr) return fArr[0].label
    }
    return null
  }

  const findTextFromDistionry = (code) => '数据字典'

  const render = () => {
    let fieldValue = withText ? text : value
    if (withText) return <label>{fieldValue}</label>

    if (controlType === TYPE.INPUT_PASSWORD) {
      fieldValue = '********'
    } else if (
      controlType === TYPE.CHECKBOX ||
      controlType === TYPE.RADIO ||
      controlType === TYPE.SELECT
    ) {
      fieldValue = options
        ? findTextFromOptions(value, options)
        : codeKey
        ? findTextFromDistionry(value)
        : value
    } else if (controlType === TYPE.DATE_PICKER) {
      fieldValue = value ? moment(value).format(FORMAT.DATE) : ''
    } else if (controlType === TYPE.TIME_PICKER) {
      fieldValue = value ? moment(value).format(FORMAT.TIME) : ''
    }
    return <label>{fieldValue}</label>
  }
  return <>{render()}</>
}

export default FieldText
